package com.designpatterns.behavioral.composite;

import java.util.ArrayList;
import java.util.List;

public class Folder extends AbstractFile {
    //定义集合fileList，用于存储AbstractFile类型的成员
    private List<AbstractFile> fileList = new ArrayList<>();

    public Folder(String name) {
        super(name);
    }

    @Override
    public void add(AbstractFile file) {
        fileList.add(file);
    }

    @Override
    public void remove(AbstractFile file) {
        fileList.remove(file);
    }

    @Override
    public AbstractFile getChild(int i) {
        return fileList.get(i);
    }

    @Override
    public void killVirus() {
        //模拟杀毒
        System.out.println("----对文件夹'" + this.getName() + "'进行杀毒");

        //递归调用成员构建的killVirus()方法
        fileList.forEach(AbstractFile::killVirus);
    }
}
